var ctx;
var pwdReg = /^(?![^a-zA-Z]+$)(?!\D+$).{6,20}$/; //至少包含字母数字的验证

function setCtx(ctx) {
	this.ctx = ctx;
}

//验证旧密码
function checkOldPwd(){
	var oldPwd = $("#oldPwd").val();
	//解决ie9以下placeholder与value重复的问题
	if(oldPwd == $("#oldPwd").attr("placeholder")){
		oldPwd = "";
	}

	if(oldPwd == null || oldPwd == ""){
		layer.tips('请输入密码！', '#oldPwd', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证新密码
function checkNewPwd(){
	var newPwd = $("#newPwd").val();
	//解决ie9以下placeholder与value重复的问题
	if(newPwd == $("#newPwd").attr("placeholder")){
		newPwd = "";
	}
	if(newPwd == null || newPwd == ""){
		layer.tips('请输入密码！', '#newPwd', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		if(!pwdReg.test(newPwd)){
			layer.tips('密码格式为长度6-20位且必须包含字母和数字!', '#newPwd', {
			    tips: 3,
			    time:1000
			});
			return false;
		}else{
			return true;
		}
	}	
}

//再次验证新密码
function checkNewPwd1(){
	var newPwd1 = $("#newPwd1").val();
	var newPwd = $("#newPwd").val();
	//解决ie9以下placeholder与value重复的问题
	if(newPwd1 == $("#newPwd1").attr("placeholder")){
		newPwd1 = "";
	}

	if(newPwd1 == null || newPwd1 == ""){
		layer.tips('请输入密码', '#newPwd1', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		if(!pwdReg.test(newPwd1)){
			layer.tips('密码格式为长度6-20位且必须包含字母和数字!', '#newPwd1', {
			    tips: 3,
			    time:1000
			});
			return false;
		}else{
			if(newPwd != newPwd1){
				layer.tips('确认密码输入不一致，请检查输入的密码!', '#newPwd1', {
				    tips: 3,
				    time:1000
				});
				return false;
			}else{
				return true;
			}
		}
	}
}

//修改密码
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
console.info(window.name);
function savePwd (){
	var userId = $("#userId").val();
	var loginName = $("#loginName").val();
	var oldPwd = $("#oldPwd").val();
	var newPwd = $("#newPwd").val();
	if(checkOldPwd() && checkNewPwd() && checkNewPwd1()){

		oldPwd = encrypt(oldPwd,loginName);
		newPwd = encrypt(newPwd,loginName);
	
		$.get(ctx+"/user/savePassword",{
			id : userId,
			lname : loginName,
			oldPwd : oldPwd,
			newPwd : newPwd
		},function(data){
			data = JSON.parse(data);
			if(data.success){
				layer.msg("操作成功",{
					icon: 1,
					time: 1000 //2秒关闭（如果不配置，默认是3秒）
				},function(){
					$.get(ctx+"/exist",{},function(data){
						data = JSON.parse(data);
						if(data.success){
							location.href = ctx;
						}
					});
					parent.layer.close(index); //根据窗口索引关闭窗口   
				});
			} else {
				layer.msg(data.msg,{
					icon: 2,
					time: 1000 //2秒关闭（如果不配置，默认是3秒）
				},function(){
					//parent.layer.close(index); //根据窗口索引关闭窗口   
				});
			}
		});
	}
}
